<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <style>
        .form-control {
            width: 250px;
        }
    </style>
</head>

<body>
    <div id="content"></div>
    <template id="tem1">
        <div>
            <h2>编辑页面</h2>
            <el-form ref="form" label-width="80px">
                <el-form-item label="姓名：">
                    <el-input class="inp" v-model="users.username"></el-input>
                </el-form-item>
                <el-form-item label="年龄：">
                    <el-input class="inp" v-model="users.age"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="save()">确认</el-button>
                    <el-button @click="ret()">返回</el-button>
                </el-form-item>
            </el-form>
        </div>
    </template>
    <script src="./js/vue.min.js"></script>
    <script src="./js/jquery.min.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script>
        var vm = new Vue({
            el: '#content',
            template: '#tem1',
            data: function () {
                return {
                    users: {
                        username: '',
                        age: '',
                        id: location.hash.replace('#', '')
                    }
                }
            },
            // 设置created
            created: function () {
                this.edit()
            },
            methods: {
                // 获取数据
                edit: function () {
                    var _this = this
                    $.get('/Users/find', { id: this.users.id }).done(function (result) {
                        _this.users.username = result.username;
                        _this.users.age = result.age;
                        _this.users.id = result.id
                    })
                },
                // 回填数据
                save: function () {
                    var _this = this;
                    $.post('/users/edit', {
                        username: _this.users.username,
                        age: _this.users.age,
                        id: _this.users.id
                    }).done(function () {
                        location.href = './elindex.html'
                    })
                },
                ret: function () {
                    location.href = './elindex.html'
                }
            }
        })
    </script>
</body>

</html>